<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class FinanceRechargeOrder extends BaseModel
{
    protected $table = 'finance_recharge_order';
    protected $connection = 'api';//api库
    public $timestamps = false;
    protected $primaryKey = 'order_id';

    public function getPayTimestampAttribute($value){
        if(!$value) return 0;
        return date('Y-m-d H:i:s',$value);
    }

    public function getCreateTimestampAttribute($value){
        if(!$value) return 0;
        return date('Y-m-d H:i:s',$value);
    }

    public function getPriceAttribute($value){
        return $value/100;
    }

    private function getPayStatus(int $status)
    {
        if ($status == 0) {
            return '还未支付';
        } elseif ($status == 1) {
            return '成功';
        } elseif ($status == 2) {
            return '已退款';
        } else {
            return '';
        }
    }
    /**
     * 获取充值列表
     */
    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $uuid = $request->input('uuid','');
        $nickname = $request->input('nickname','');
        $order_sn = $request->input('order_sn', '');
        $out_trade_sn = $request->input('out_trade_sn','');
        $begin_date = strtotime($request->input('begin_date',0));
        $end_date = strtotime($request->input('end_date',0));
        $status = $request->input('status',1);
        $user_id = $request->input('user_id', null);
        $payType = $request->input('pay_type', '');//支付类型 20=云闪付

        $query = new self();

        $query = $query->whereHas('numbers',function ($query) use ($uuid, $nickname){
            $query->select(['user_id']);
            if($uuid) $query->where('uuid',$uuid);
            if($nickname) $query->where('nickname','like',"%{$nickname}%");
        });
        if($out_trade_sn) $query = $query->where('out_trade_sn','like',"%{$out_trade_sn}");
        if($begin_date && $end_date) $query = $query->whereBetween('pay_timestamp',[$begin_date, $end_date]);
        if($begin_date && !$end_date) $query = $query->where('pay_timestamp','>=', $begin_date);
        if(!$begin_date && $end_date) $query = $query->where('pay_timestamp','<=', $end_date);
        if ($status != '') {
            if($status >= 0) {
                $query = $query->where('status', $status);
            } elseif($status == -2) {
                $query = $query->whereNotIn('status', [0,1]);
            }
        }

        if(isset($user_id)) $query = $query->where('user_id', $user_id);
        if ($order_sn) {
            $query = $query->where('order_sn', $order_sn);
        }

        if ($payType) {
            $query = $query->where('pay_type', $payType);
        }

        $list = $this->objToArray($query->with(['numbers:user_id,uuid,nickname,create_time'])
            ->orderBy('order_id','DESC')
            ->paginate($limit,['*'],'page',$page));
        //获取所有用户ids
        $userIds = array_column($list['data'],'user_id');
        $isfrist = DB::connection($this->connection)->table($this->table)->select(DB::raw('COUNT(status = 1 or null) as count,user_id'))->where('status',1)->whereIn('user_id',$userIds)->groupBy('user_id')->get();
        $isfrist = objToArray($isfrist);
        $isfirstArr = [];
        if(!empty($isfrist))
        {
            $isfirstArr = array_column($isfrist,'count','user_id');
        }
        foreach ($list['data'] as &$item) {
            $item['pay_status'] = $this->getPayStatus($item['status']);
            //是否是首次充值
            if(isset($isfirstArr[$item['user_id']]) && $isfirstArr[$item['user_id']] == 1){
                $item['is_first'] = 1; //首充
            }else{
                $item['is_first'] = 0; //非首充
            }
            
        }
        return $list;
    }
    /**
     * 关联关系-关联User表
     *
     * @return void
     */
    public function numbers(){
        return $this->belongsTo(Number::class,'user_id','user_id');
    }
}
